import type { LoginResult, UserData, UserResult } from '@/types/user'
import request from '@/utils/request/request'

/**
 * 用户登录
 * @param params
 * @returns
 */
export const loginAPI = (params: { phone: string; password: string }) => {
  return request<LoginResult>({
    method: 'POST',
    url: '/user/login',
    data: params,
  })
}
/**
 * 获取验证码
 */
export const getCodeAPI = ( phone: string ) => {
  return request({
    method: 'POST',
    url: '/user/code',
    data: phone,
  })
}
/**
 * 验证码登录
 */
export const codeLoginAPI = (params: { phone: string; code: string }) => {
  return request<LoginResult>({
    method: 'POST',
    url: '/user/login/code',
    data: params,
  })
}
/**
 * 退出登录
 */
export const logoutAPI = () => {
  return request({
    method: 'POST',
    url: '/user/logout',
  })
}
/**
 * 用户注册
 * @param params
 * @returns
 */
export const registerAPI = (params: { phone: string; password: string }) => {
  return request({
    method: 'POST',
    url: '/user/register',
    data: params,
  })
}
/**
 * 根据id查询用户信息
 * @param id
 * @returns
 */
export const getUserAPI = (id: number) => {
  return request<UserResult>({
    method: 'GET',
    url: `/user/${id}`,
  })
}
/**
 * 修改用户信息
 * @param params
 */
export const putUserAPI = (params: UserData) => {
  return request({
    method: 'PUT',
    url: '/user',
    data: params,
  })
}
/**
 * 修改用户头像
 * @param params
 */
export const putAvatarAPI = (params: { id: number; avatar: string }) => {
  return request({
    method: 'PUT',
    url: '/user/avatar',
    params: params,
  })
}
